/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package projectshanghai;

import Actor.Player;
import Actor.enemies.Henchman;
import Map.Map;
import Map.MapLoader;
import java.awt.Color;
import java.awt.Cursor;
import java.awt.Graphics;
import java.awt.event.*;
import java.io.File;
import javax.swing.JPanel;
import javax.swing.Timer;

/**
 *
 * @author Eric-Standard
 */
public class GamePanel extends JPanel implements ActionListener, KeyListener,
        MouseListener {

    private Map map = MapLoader.loadMap(new File("testMap.txt"));
    //Update at about 60 Hz
    public static final long FRAME_RATE = 1000 / 60;
    private long previousTime = 0;
    public final Timer t = new Timer((int) FRAME_RATE, this);
    public static final int PANEL_WIDTH = Map.TILE_WIDTH * Map.MAP_WIDTH + 2 * World.HUD_WIDTH;
    public static final int PANEL_HEIGHT = Map.TILE_WIDTH * Map.MAP_WIDTH + 2 * World.HUD_HEIGHT;
    //private ArrayList<Actor> actors = new ArrayList<Actor>();
    private Player player = new Player();
    private World world = new World(PANEL_WIDTH, PANEL_HEIGHT);

    public GamePanel() {
        setSize(GameFrame.FRAME_WIDTH, GameFrame.FRAME_HEIGHT);
        setFocusable(true);
        //Set listener
        addKeyListener(player);
        addMouseListener(world.mouseMonitor);
        addMouseMotionListener(world.mouseMonitor);
        //Set cursor to crosshair
        this.setCursor(new Cursor(Cursor.CROSSHAIR_CURSOR));
        setBackground(Color.black);
        //Add some actors
        world.add(player);
        world.setPlayer(player);
        world.add(new Henchman());
        Henchman henchman = new Henchman();
        henchman.getPos().x = 20;
        world.add(henchman);
        world.loadMap(new File("testMap.txt"));
        t.start();
    }

    @Override
    public void paintComponent(Graphics g) {
        super.paintComponent(g);
        world.draw(g);
    }

    @Override
    public void actionPerformed(ActionEvent e) {
        long currentTime = System.currentTimeMillis();
        long dt = currentTime - previousTime;
        if (dt < FRAME_RATE) {
            return;
        }
        //Update the world
        world.step(dt);
        repaint();
        previousTime = currentTime;
    }

    @Override
    public void keyTyped(KeyEvent e) {
    }

    @Override
    public void keyPressed(KeyEvent e) {
        int keyCode = e.getKeyCode();
        switch (keyCode) {
            case KeyEvent.VK_UP:
            case KeyEvent.VK_DOWN:
            case KeyEvent.VK_LEFT:
            case KeyEvent.VK_RIGHT:
                player.keyPressed(e);
                break;
            default:
                player.keyPressed(e);
                break;
        }
    }

    @Override
    public void keyReleased(KeyEvent e) {
        int keyCode = e.getKeyCode();
        switch (keyCode) {
            case KeyEvent.VK_UP:
            case KeyEvent.VK_DOWN:
            case KeyEvent.VK_LEFT:
            case KeyEvent.VK_RIGHT:
                player.keyReleased(e);
                break;
            default:
                player.keyReleased(e);
                break;
        }
    }

    @Override
    public void mouseClicked(MouseEvent e) {
    }

    @Override
    public void mousePressed(MouseEvent e) {
    }

    @Override
    public void mouseReleased(MouseEvent e) {
    }

    @Override
    public void mouseEntered(MouseEvent e) {
    }

    @Override
    public void mouseExited(MouseEvent e) {
    }
}
